<?php
class Common extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    /**
     * 积分增减记录
     * int $user_id 用户id
     * str $type 类型
     * int $coin 积分
     * str $intro 简介
     * array $data 简要数据
     */
    function add_coin_log($user_id,$type,$coin,$intro,$data=array())
    {
    	$insert = array();
		$insert['user_id'] = $user_id;
		$insert['type'] = $type;
		$insert['coin'] = $coin;
		$insert['data'] = count($data)>0?serialize($data):'';
		$insert['add_time'] = time();
		$this->db->insert('coin_log',$insert);
		return true;
    }
    /**
     * 积分增减
     * int $user_id 用户id
     * str $type 类型 regist(注册)、sign_in(签到)
     * int $coin 积分
     * array $data 简要数据
     */
    function add_coin($user_id,$type,$coin,$data=array())
    {
    	if( $coin<0 ){
    		$reduce_coin = abs($coin);
    		$this->db->set('coin', "'coin-{$reduce_coin}'", FALSE);
    	}else{
    		$this->db->set('coin', "coin+{$coin}", FALSE);
    		$this->db->set('max_coin', "max_coin+{$coin}", FALSE);
    	}
    	$this->db->where('id',$user_id)->limit(1)->update('user');
    	$this->add_coin_log($user_id,$type,$coin,$data);
		return true;
    }
}
?>
